package leetcode.code0623;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	int aim;

	public TreeNode addOneRow(TreeNode root, int val, int depth) {
		if (depth == 1) {
			TreeNode node = new TreeNode(val);
			node.left = root;
			return node;
		}
		aim = depth - 1;
		return this.add(root, val, 1);
	}

	private TreeNode add(TreeNode root, int val, int index) {
		if (root == null) {
			return null;
		}
		if (index == aim) {
			TreeNode left = root.left;
			TreeNode right = root.right;
			TreeNode l = new TreeNode(val);
			TreeNode r = new TreeNode(val);
			root.left = l;
			root.right = r;
			l.left = left;
			r.right = right;
		} else {
			root.left = this.add(root.left, val, index + 1);
			root.right = this.add(root.right, val, index + 1);
		}
		return root;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
